package com.fic.dao;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import oracle.jdbc.OracleTypes;
import utiles.AppProperties;
import utiles.ConexionPool;

import com.fic.dto.MedicamentoDTO;
import com.tham.dao.AgendaDAO;

public class FicMedicamentoDAO {
	
	private static Logger log = Logger.getLogger(AgendaDAO.class);
	String pkgVar = (AppProperties.getProperty("aplicacion", "PACKAGE"));

	public List<MedicamentoDTO> getMedicamentos(String Medicamento, String Estado, String Origen, String idMedicamento) throws SQLException, IOException{
		Connection conn = ConexionPool.getConnectionPool();	
		List<MedicamentoDTO> listCombo = new ArrayList<MedicamentoDTO>();
		
		try {
			String query = "{ call "+pkgVar+".PKG_FIC_MEDICAMENTOS.getMedicamentos(?, ?, ?, ?, ?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, Medicamento);
			stmt.setString(2, Estado);
			stmt.setString(3, Origen);
			stmt.setString(4, idMedicamento);
			stmt.registerOutParameter(5, OracleTypes.CURSOR);
			stmt.execute();
			ResultSet rs = (ResultSet)stmt.getObject(5);

			while (rs.next()) {
				MedicamentoDTO medDTO = new MedicamentoDTO();
				medDTO.setIdMedicamento(rs.getString(1));
				medDTO.setDescMedicamento(rs.getString(2));
				medDTO.setEstado(rs.getString(3));
				medDTO.setEstado1(rs.getString(3));
				medDTO.setFechaDigitacion(rs.getString(4));
				medDTO.setIdUsuarioDigitacion(rs.getString(5));
				medDTO.setFechaModificacion(rs.getString(6));
				medDTO.setIdUsuarioModificacion(rs.getString(7));
				listCombo.add(medDTO);
			}
			rs.close();
			stmt.close();
		} catch (SQLException ex) {  
			log.error("Error FicMedicamentoDAO.getMedicamentos SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			//ConexionPool.desconectaPool(conn);
			conn.close();
			conn = null;
		}
		return listCombo;

	}	
	
	public List<MedicamentoDTO> setMedicamentos(String idMedicamento, String listaIds, String strMedicamento) throws SQLException, IOException{
		Connection conn = ConexionPool.getConnectionPool();	
		List<MedicamentoDTO> listCombo = new ArrayList<MedicamentoDTO>();
		
		try {
			String query = "{ call "+pkgVar+".PKG_FIC_MEDICAMENTOS.setMedicamentos(?, ?, ?, ?, ?, ?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, idMedicamento);
			stmt.setString(2, listaIds);
			stmt.setString(3, strMedicamento);
			stmt.registerOutParameter(4, OracleTypes.NUMBER);
			stmt.registerOutParameter(5, OracleTypes.NUMBER);
			stmt.registerOutParameter(6, OracleTypes.NUMBER);
			stmt.execute();
			MedicamentoDTO medDTO = new MedicamentoDTO();
			medDTO.setCantMedicamentos(stmt.getString(4));
			medDTO.setCantTratamientos(stmt.getString(5));
			medDTO.setCantEliminados(stmt.getString(6));
			listCombo.add(medDTO);
			stmt.close();
		} catch (SQLException ex) {  
			log.error("Error FicMedicamentoDAO.setMedicamentos SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			//ConexionPool.desconectaPool(conn);
			conn.close();
			conn = null;
		}
		return listCombo;
	}			
	
	public List<MedicamentoDTO> getMedicamentosEvolucion(String idMedicamento) throws SQLException, IOException{
		Connection conn = ConexionPool.getConnectionPool();	
		List<MedicamentoDTO> listCombo = new ArrayList<MedicamentoDTO>();
		
		try {
			String query = "{ call "+pkgVar+".PKG_FIC_MEDICAMENTOS.getMedicamentosEvolucion(?, ?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, idMedicamento);
			stmt.registerOutParameter(2, OracleTypes.CURSOR);
			stmt.execute();
			ResultSet rs = (ResultSet)stmt.getObject(2);

			while (rs.next()) { 
				MedicamentoDTO medDTO = new MedicamentoDTO();
				medDTO.setFechaDigitacion(rs.getString(1));
				medDTO.setRutPac(rs.getString(2));
				medDTO.setNombrePac(rs.getString(3)); 
				medDTO.setDescMedicamento(rs.getString(4));
				medDTO.setDosis(rs.getString(5));
				medDTO.setCantidad(rs.getString(6));
				medDTO.setVia(rs.getString(7));
				medDTO.setTiempo(rs.getString(8));
				listCombo.add(medDTO);
			}
			rs.close();
			stmt.close();
		} catch (SQLException ex) {  
			log.error("Error FicMedicamentoDAO.getMedicamentosEvolucion SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			//ConexionPool.desconectaPool(conn);
			conn.close();
			conn = null;
		}
		return listCombo;

	}		
	
}
